package com.SouthernWall_404.age_tool.common.Network.Packets.C2S.HandShake;

import com.SouthernWall_404.age_tool.common.Network.ModChannels;
import com.SouthernWall_404.age_tool.common.Network.Packets.S2C.MusicBlockStartPlayS2CPacket;
import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.level.Level;
import net.minecraftforge.network.NetworkEvent;

import java.util.function.Supplier;

public class MusicBlockStartPlayHandShakePacket {
    private BlockPos blockPos;
    public MusicBlockStartPlayHandShakePacket(BlockPos blockPos)
    {
        this.blockPos=blockPos;
    }

    public MusicBlockStartPlayHandShakePacket(FriendlyByteBuf buf) {
        this.blockPos=buf.readBlockPos();
    }

    public void encode(FriendlyByteBuf buf) {
        buf.writeBlockPos(this.blockPos);
    }

    public boolean handle(Supplier<NetworkEvent.Context> supplier)
    {
        var content=supplier.get();
        content.enqueueWork(() -> {

            Level level=content.getSender().level();

            if(level.hasNeighborSignal(blockPos))
            {
                ModChannels.sendToClient(new MusicBlockStartPlayS2CPacket(false,this.blockPos),content.getSender());
            }
            else
            {
                ModChannels.sendToClient(new MusicBlockStartPlayS2CPacket(true,this.blockPos),content.getSender());
            }

        });


        content.setPacketHandled(true);
        return true;
    }
}
